package com.quickcode.example.lock;

import java.util.concurrent.locks.StampedLock;

/**
 * 乐观锁
 */
public class StampedLockExample {

    private final static StampedLock STAMPED_LOCK = new StampedLock();

    private static String str = "123456";

    public static void main(String[] args) {

        System.out.println(str);

        long value = STAMPED_LOCK.tryOptimisticRead();
        System.out.println("value:" + value);

        long readValue = STAMPED_LOCK.readLock();
        System.out.println("readValue:" + readValue);
        System.out.println("value valid:" + STAMPED_LOCK.validate(value));

        STAMPED_LOCK.unlockRead(readValue);

        long writeValue = STAMPED_LOCK.writeLock();
        System.out.println("writeValue:" + writeValue);
        STAMPED_LOCK.unlock(writeValue);
        System.out.println("value valid:" + STAMPED_LOCK.validate(value));

    }

}